﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DBCodeGenerator
{
    public class UserSettings
    {
        public List<string> Getallsettings()
        {

            List<string> allsettings = new List<string>();
            FileStream fs = null;
            StreamReader sr = null;
            try
            {
                fs = File.Open("lastconfig.ini", FileMode.OpenOrCreate);
                sr = new StreamReader(fs);
                while (!sr.EndOfStream)
                {
                    string ip = sr.ReadLine();
                    if (ip.Trim() != "")
                        allsettings.Add(ip.Trim());
                }
            }
            catch (Exception ex)
            {

            }
            finally
            {
                if (sr != null)
                    sr.Close();

                if (fs != null)
                    fs.Close();


            }

            return allsettings;
        }

        public void SaveSettings(string ipaddress)
        {

            List<string> allsettings = Getallsettings();
            FileStream fs = null;
            StreamWriter sw = null;
            string ip = "";
            try
            {
                if (allsettings != null)
                     ip = allsettings.Find(p =>p.Contains(ipaddress));
                
                if (String.IsNullOrEmpty(ip))
                {
                    fs = File.Open("lastconfig.ini", FileMode.Append);
                    sw = new StreamWriter(fs);
                    sw.WriteLine(ipaddress.Trim());
                }
            }
            catch (Exception ex)
            {

            }
            finally
            {
                if (sw != null)
                    sw.Close();

                if (fs != null)
                    fs.Close();
            }

        }
    }
}
